package com.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mysql.fabric.xmlrpc.base.Data;
//import com.sun.xml.internal.txw2.output.DumpSerializer;
import com.test.model.Users;
import com.test.service.UserService;

import java.util.Date;
import java.util.List;

@Path("/user")
public class UserController extends Controller {

    public void index(){
        render("user.html");
    }
    public void list(){
        int page= getParaToInt("page");
        int limit= getParaToInt("limit");
        String userName = get("userName");
//        Page<Users>p = Users.dao.paginate(page,limit,"select *","from sys_users order by id desc");
        Page<Users>p = Users.dao.template("getList", Kv.by("userName",userName)).paginate(page,limit);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","操作成功");
        jsonObject.put("count",p.getTotalRow());
        jsonObject.put("data",p.getList());
        renderJson(jsonObject);

    }

   public void add(String userName,String money,int gender){
        Users users=new Users();
        users.setUserName(userName);
        users.setMoney(money);
        users.setGender(gender);
        users.setPassWord("123");
        users.setCreateTime(new Date());
        users.save();

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","添加成功");
        renderJson(jsonObject);
    } 
        //修改信息
        public void update(){
        String id=getPara("id");
        String userName=getPara("userName");
        String money=getPara("money");
        int gender=getParaToInt("gender");
        Users users =Users.dao.findById(id);
        users.setUserName(userName);
        users.setMoney(money);
        users.setGender(gender);
        users.update();

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","修改成功");
        renderJson(jsonObject);
    }   
       //删除信息
      public void del(){
        String id=getPara("id");
        String ids=getPara("ids");
        if(StrKit.notBlank(id)){
            Users.dao.deleteById(id);
        }else{
            String [] idArra=ids.split(",");
            for (String myId:idArra){
                Users.dao.deleteById(myId);
            }
        }

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","删除成功");
        renderJson(jsonObject);
    }

    public void updatepwd(){
        String id=getPara("id");
        String passWord = get("passWord");

        Users user1 = getSessionAttr("user");


        Users user =Users.dao.findById(user1.getId());
        user.setPassWord(passWord);

        user.update();

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","重置成功");
        renderJson(jsonObject);
    }
}
